package com.zmht.beike.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.beike.po.TCweekcontent;
import com.zmht.beike.po.TCweekinfo;
import com.zmht.specedu.vo.Teacher;

public class CweekinfoAction extends BaseAction {

	/*
	 * 从月主题管理跳转到周主题
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward initCweekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//System.out.println("转发到周主题页面 。。。"); 传入 月主题ID
		String monthId =request.getParameter("monthID");
		String areaId = request.getParameter("areaId");
		TCmonthinfo monthinfo = this.getCmonthinfoBiz().findTcmonth(new Integer(monthId));
		
		request.getSession().setAttribute("cmonthinfo", monthinfo);
		request.setAttribute("mthinfo", monthinfo);	
		request.setAttribute("areaId", areaId);
		return mapping.findForward("addZhouzhuti");
	}
	/**
	 * 获得所有周单元目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getCweekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
			
			//开始分页查询
			Integer index = 1;
			Integer pageSize = 5;
			if(null!=request.getParameter("currPage")){
				index = new Integer(request.getParameter("currPage"));
			}
			if(null!=request.getParameter("pageSize")){
				pageSize = new Integer(request.getParameter("pageSize"));
			}
			
			List list = this.getCweekinfoBiz().getCweekinfo(teacher.getTeacherId(), (index-1)*pageSize, pageSize);
			int total = this.getCweekinfoBiz().getCweekinfoToatl(teacher.getTeacherId());
			
			
			request.setAttribute("currPage", index); 
			request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
			request.setAttribute("pageSize", pageSize);
			request.setAttribute("monthinfo", list);
			System.out.println("周单元目标 个数："+list.size());
		
		return mapping.findForward("weekManagement");
	}
	/**
	 * 添加周单元目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addCweekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
			//String week = request.getParameter("week");
			//String weekThemeName = request.getParameter("weekThemeName");//周主题名称
			//获得月主题
			TCmonthinfo monthinfo = (TCmonthinfo)request.getSession().getAttribute("cmonthinfo");//this.getCmonthinfoBiz().findTcmonth(new Integer(cmonthId));
			TCaiminfo caiminfo = (TCaiminfo)request.getSession().getAttribute("caiminfo");
			if(caiminfo == null){
				
				List<TCaiminfo> list = this.getCweekinfoBiz().findByHQL("from TCaiminfo t where t.id = "+request.getParameter("targetId"));
				caiminfo = list.get(0);
			}
			//System.out.println("=================caiminfoid=========="+caiminfo.getId());
			this.saveCweekInfo(teacher.getTeacherId(), monthinfo,caiminfo);
			//System.out.println("=================caiminfoid=========="+caiminfo.getId());
		
		return new ActionForward("/beike/cweekinfo.do?method=getCweekinfo");
	}
	private boolean saveCweekInfo(Integer teacherId,TCmonthinfo monthinfo,TCaiminfo caiminfo){
		for(int m=1;m<11;m++){ //月份
			for(int i=1;i<5;i++){
				List<TCweekcontent> list = this.getCweekcontentBiz().getCweekContents(teacherId,i,m, 0, 100);
				if(list!=null&&list.size()>0){
					TCweekinfo weekinfo = new TCweekinfo();
					weekinfo.setTeacherId(teacherId);
					//weekinfo.setCmonthId(cmonthId);//月主题ID
					weekinfo.setWeekThemeName(list.get(0).getWeekThemeName());
					weekinfo.setMonthThemeName(monthinfo.getMonthThemeName());
					weekinfo.setMonth(monthinfo.getMonth());
					weekinfo.setCmonthId(monthinfo.getId());
					weekinfo.setCaimId(caiminfo.getId());
					weekinfo.setCareaId(monthinfo.getCareaId());
					weekinfo.setTerm(caiminfo.getTerm());
					//保存后获得id
					Integer id  = 0;
					String hql = "from TCweekcontent t where t.teacherId='"+weekinfo.getTeacherId()+"' and t.caimId="+weekinfo.getCaimId()+" and t.month="+weekinfo.getMonth()+" and t.week="+i;
					List<TCweekcontent> weekinfos = this.getCweekinfoBiz().findByHQL(hql);
					
					if(null!=weekinfos&&weekinfos.size()>0){
						id = weekinfos.get(0).getCweekId();
						if(null==id){
							id = this.getCweekinfoBiz().saveCweekinfo(weekinfo);
						}
					}else{
						id = this.getCweekinfoBiz().saveCweekinfo(weekinfo);
					}
					for(TCweekcontent t:list){					
						t.setCweekId(id);
						t.setMonthThemeName(monthinfo.getMonthThemeName());
						t.setCmonthId(monthinfo.getId());//月主题id
						this.getCweekcontentBiz().updateCweekContent(t);
					}
				}
			
			}
		}
		return true;
	}
	/**
	 * 初始化周内容编辑页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initCweekinfoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		String week = request.getParameter("week");
		String areaId = request.getParameter("areaId");
		
		//TCmonthinfo monthinfo = this.getCmonthinfoBiz().findTcmonth(new Integer(monthId));
		
		//request.getSession().setAttribute("cmonthinfo", monthinfo);
		//request.setAttribute("mthinfo", monthinfo);		
		//return mapping.findForward("addZhouzhuti");
		request.setAttribute("targetId", targetId);
		request.setAttribute("month", month);
		request.setAttribute("week", week);
		request.setAttribute("areaId", areaId);
		return new ActionForward("/jsp/mubiaozhiding/edit_Zhouzhuti.jsp");
	}
	/**
	 * 编辑目标完成时 直接跳转
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editCweekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		return new ActionForward("/beike/cweekinfo.do?method=getCweekinfo");
	}
}
